<?php

class TestDbQueryTplStorage extends UnitTestCase {

    /*var $cfg=array(
        'storage'=>array(
            'search'=>'tests/{{id}}.mockstorable.txt'
            ,'class'=>'MockStorable'
            ,'id_member'=>'id'));*/

    function setUp() {
        $cfg=array(
            'storage'=>array(
                'filenames'=>'tests/{{id}}.querytpl.sql'
            ), 'criteria'=>array());
        $this->S=DbQueryTplStorage::toolFactory('webappkit.db.querytpl',$cfg);
        $this->path=Webappkit::getPathFromId('webappkit.db.querytpl').'tests/';
    }

    /**
     * test de recherche
     * /
    function testSearch() {
        $S=WakStorage::Factory('webappkit.storages',$this->cfg);
        $this->assertTrue($S->has('alpha'));
        $obj=$S->get('alpha');
        $this->assertIsA($obj,'MockStorable');
        // echo '<pre>'; print_r($S);
    }/*

    /**
     * simple sequence
     */
    function testSimpleSequence() {
        // building an instance
        $id='users.get';
        $src="SELECT * FROM users WHERE login={{login}}";
        $tpl=new DbQueryTpl($src,$id);
        // saving
        $this->assertTrue($this->S->save($tpl)
            ,"save() should return true");
        // checking saved as clear
        $file=$this->path.$id.'.querytpl.sql';
        $this->assertTrue(
            file_exists($file)
            ,"should have saved as $file");
        $this->assertEqual(
            file_get_contents($file),$src
            ,"QueryTpl source should have been saved in clear");
        $this->assertTrue($this->S->has($id)
            ,"has() should now return true");
        // getting a copy and check
        if (!$copy=$this->S->get($id)) {
            $this->fail("could not get a copy of inserted object %s");
            return false;
        }
        $this->assertEqual($tpl,$copy,"get() : retrieved copy should equal saved object");
        //$this->dump($object);
        // modifying copy, saving
        $tpl->setSource("SELECT * FROM users WHERE id={{id}}");
        $this->S->save($copy);
        if (!$copy2=$this->S->get($id)) {
            $this->fail("could not get a copy of inserted object %s");
            return false;
        }
        $this->assertEqual($copy,$copy2,"retrieved copy should equal saved changes");
        // deleting
        $this->assertTrue($this->S->delete($id)
            ,"delete(id) should return true");
        $this->assertFalse($this->S->has($id)
            ,"has(id) should return false after deletion");
    }
}
